﻿@{
    Layout = "~/Administration/Views/Shared/_AdminPopupLayout.cshtml";
}
@model CustomerRoleModel.AssociateProductToCustomerRoleModel
@{
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;

    //page title
    ViewBag.Title = T("Admin.Customers.CustomerRoles.Fields.PurchasedWithProduct.Choose").Text;
}
@using (Html.BeginForm())
{
    <div class="section-header">
        <div class="title">
            <img src="@Url.Content("~/Administration/Content/images/ico-catalog.png")" alt="" />
            @T("Admin.Customers.CustomerRoles.Fields.PurchasedWithProduct.Choose")
        </div>
    </div>    
    
    if (ViewBag.RefreshPage == true)
    {
    <script type="text/javascript">
    try {
        window.opener.document.getElementById('@(ViewBag.productIdInput)').value = '@(ViewBag.productId)';
        window.opener.document.getElementById('@(ViewBag.productNameInput)').innerHTML = '@(ViewBag.productName)';
        window.opener.document.getElementById('@(ViewBag.btnId)').click();
    }
    catch (e){}
    window.close();
    </script>
    }

    @Html.HiddenFor(model => model.AssociatedToProductId)
    <table width="100%">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchProductName):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SearchProductName)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchCategoryId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchCategoryId", Model.AvailableCategories)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchManufacturerId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchManufacturerId", Model.AvailableManufacturers)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchStoreId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchStoreId", Model.AvailableStores)
            </td>
        </tr>
        <tr @(Model.IsLoggedInAsVendor ? Html.Raw("style='display: none;'") : null)>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchVendorId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchVendorId", Model.AvailableVendors)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchProductTypeId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchProductTypeId", Model.AvailableProductTypes)
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" id="search-products" class="k-button" value="@T("Admin.Common.Search")" />
            </td>
        </tr>
    </table>
    <p>
    </p>
    <table class="adminContent">
        <tr>
            <td>
                <div id="products-grid"></div>

                <script>
                    $(document).ready(function () {
                        $("#products-grid").kendoGrid({
                            dataSource: {
                                type: "json",
                                transport: {
                                    read: {
                                        url: "@Html.Raw(Url.Action("AssociateProductToCustomerRolePopupList", "CustomerRole"))",
                                        type: "POST",
                                        dataType: "json",
                                        data: additionalData
                                    }
                                },
                                schema: {
                                    data: "Data",
                                    total: "Total",
                                    errors: "Errors"
                                },
                                error: function(e) {
                                    display_kendoui_grid_error(e);
                                    // Cancel the changes
                                    this.cancelChanges();
                                },
                                pageSize: @(gridPageSize),
                                serverPaging: true,
                                serverFiltering: true,
                                serverSorting: true
                            },
                            pageable: {
                                refresh: true
                            },
                            editable: {
                                confirmation: false,
                                mode: "inline"
                            },
                            scrollable: false,
                            columns: [{
                                field: "Name",
                                title: "@T("Admin.Common.Select")",
                                width: 50,
                                template: '<input type="button" value="@T("Admin.Common.Select")" onclick="selectAssociatedProduct(#=Id#)" class="k-button" />'
                            },{
                                field: "Name",
                                title: "@T("Admin.Catalog.Products.Fields.Name")"
                            }, {
                                field: "Published",
                                title: "@T("Admin.Catalog.Products.Fields.Published")",
                                width: 100,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                                template: '<img src="@Url.Content("~/Administration/Content/images/")active-#=Published#.gif" />'
                            }]
                        });
                    });
                </script>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" id="save" name="save" class="k-button" value="@T("Admin.Common.Save")" style="display: none" />
            </td>
        </tr>
    </table>
    
    <script type="text/javascript">
        $(document).ready(function () {
            $('#search-products').click(function () {
                var grid = $('#products-grid').data('kendoGrid');
                grid.dataSource.page(1); //new search. Set page size to 1
                grid.dataSource.read();
                return false;
            });
        });
        
        $("#@Html.FieldIdFor(model => model.SearchProductName)").keydown(function (event) {
            if (event.keyCode == 13) {
                $("#search-products").click();
                return false;
            }
        });

        function additionalData() {
            return {
                SearchProductName: $('#@Html.FieldIdFor(model => model.SearchProductName)').val(),
                SearchCategoryId: $('#SearchCategoryId').val(),
                SearchManufacturerId: $('#SearchManufacturerId').val(),
                SearchStoreId: $('#SearchStoreId').val(),
                SearchVendorId: $('#SearchVendorId').val(),
                SearchProductTypeId: $('#SearchProductTypeId').val()
            };
        }
        
        function selectAssociatedProduct(productid) {
            $("#@Html.FieldIdFor(model => model.AssociatedToProductId)").val(productid);
            $('#save').click();
        }
    </script>
}